<template>
	<div>
		<div class="jumbotron">
			<form class="form-horizontal" role="form" @submit.prevent="">
				<div class="form-group">
					<div class="col-sm-offset-2 col-sm-10">
						<h1>企业管理</h1>
					</div>
				</div>
				<div class="col-sm-2"></div>
				<div class="form-group table-responsive col-sm-8">
					<table class="table">
						<thead align="right">
							<tr>
								<th>账号</th>
								<th>企业名称</th>
								<th>所在地区</th>
								<th>企业信息</th>
								<th>状态</th>
								<th>操作</th>
							</tr>
						</thead>
						<tbody>
							<tr v-for="item in enterpriseInfo">
								<td>{{item.username}}</td>
								<td>{{item.entName}}</td>
								<td>{{item.region}}</td>
								<td>{{item.information}}</td>
								<td v-if="item.verified==='0'">待审核</td>
								<td v-else>审核通过</td>
								<td v-if="item.verified==='0'">
									<button type="button" class="btn btn-primary" @click="auditItem(item)">通过审核</button>
									<button type="button" class="btn btn-danger" @click="deleteItem(item)">删除信息</button>
								</td>
								<td v-else>
									<button type="button" class="btn btn-danger" @click="deleteItem(item)">删除信息</button>
								</td>
							</tr>
						</tbody>
					</table>
				</div>
			</form>
		</div>
	</div>
</template>

<script>
	module.exports = {
		mounted() {
			$.ajax({
				url: conf.ur + "/api/admin/listAllEnterprise",
				type: 'get',
				dataType: 'json',
				headers: {
					"Content-Type": "application/json",
					"x-auth-token": window.localStorage.getItem('x-auth-token')
				},
				success: (data) => {
					if (data.code === 0) {
						console.log(JSON.stringify(data))
						this.enterpriseInfo = data.data;
					} else {
						console.log("获取失败！")
					}
				},
				error: (err) => {
					console.log("网络异常！")
					conf.storage.clear();
					window.location.reload();
				}
			});
		},
		data: function() {
			return {
				enterpriseInfo: [],
				userID: '',
			}
		},
		methods: {
			auditItem(item) {
				if (confirm("确认审核通过此企业吗？") == true) {
					$.ajax({
						url: conf.ur + "/api/admin/auditEnterprise?userId=" + item.userId,
						type: 'get',
						headers: {
							"Content-Type": "application/json",
							"x-auth-token": window.localStorage.getItem('x-auth-token')
						},
						success: (data) => {
							if (data.code === 0) {
								alert("审核成功！");
								window.location.reload();
							} else {
								alert("审核失败！")
								window.location.reload();
							}
						},
						error: (err) => {
							console.log("网络异常！")
						}
					});
				}
			},
			deleteItem(item) {
				if (confirm("确认删除此企业信息吗？") == true) {
					$.ajax({
						url: conf.ur + "/api/admin/deleteEnterprise?userId=" + item.userId,
						type: 'get',
						headers: {
							"Content-Type": "application/json",
							"x-auth-token": window.localStorage.getItem('x-auth-token')
						},
						success: (data) => {
							if (data.code === 0) {
								alert("删除成功！");
								window.location.reload();
							} else {
								alert("删除失败！")
								window.location.reload();
							}
						},
						error: (err) => {
							console.log("网络异常！")
						}
					});
				}
			},
		}
	}
</script>

<style scoped>
	.goodsImg {
		height: 100px;
	}

	.jumbotron {
		background-color: ghostwhite;
	}
</style>
